package cn.lingyangwl.agile.msg.push.controller.captcha;

import cn.lingyangwl.agile.msg.push.api.model.rqrs.CheckAuthUserCaptchaReq;
import cn.lingyangwl.agile.msg.push.api.model.rqrs.SendCaptchaMsgReq;
import cn.lingyangwl.agile.msg.push.service.CaptchaMessageService;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamPersonalResource;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.security.ratelimit.LimitTypeEnum;
import cn.lingyangwl.framework.security.ratelimit.RateLimit;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 通知用户消息
 * @author shenguangyang
 */
@RestController
@RequestMapping("/authUserCaptchaMsg")
@RamPersonalResource(action = "msg:authUserVerify:message", url = "/authUserVerify", type = ActionType.WRITE, name = "发送验证消息")
public class AuthUserCaptchaMsgController {

    @Resource
    private CaptchaMessageService captchaMessageService;
    /**
     * 针对只有登录才能发送的验证消息
     */
    @PostMapping("/send")
    @RateLimit(key = "rate_limit::user_verify_message", time = 120, count = 4, limitType = LimitTypeEnum.IP,
            msg = "操作过于频繁, 请 ${remainingTime} 后再次尝试")
    public Resp<?> sendCaptcha(@Validated @RequestBody SendCaptchaMsgReq req) throws Exception {
        captchaMessageService.sendAuthUserCaptcha(req);
        return Resp.ok();
    }


    @PostMapping(value = "/check")
    public Resp<?> checkCaptcha(@RequestBody @Validated CheckAuthUserCaptchaReq req) {
        captchaMessageService.checkAuthUserCaptcha(req);
        return Resp.ok();
    }
}
